package com.wechat.controller;

import com.wechat.annotation.GlobalInterceptor;
import com.wechat.entity.dto.AppUpdateDTO;
import com.wechat.entity.po.AppUpdate;
import com.wechat.entity.query.AppUpdateQuery;
import com.wechat.entity.vo.PaginationResultVO;
import com.wechat.entity.vo.ResponseVO;
import com.wechat.service.AppUpdateService;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.io.IOException;

/**
 * app发布 Controller
 */
@RestController("adminAppUpdateController")
@RequestMapping("/admin")
@Validated
public class AdminAppUpdateController extends ABaseController {

    @Resource
    private AppUpdateService appUpdateService;

    /**
     * 根据条件分页查询版本发布情况
     */
    @GetMapping("/loadUpdateList")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO loadUpdateList(AppUpdateQuery query) {
        query.setOrderBy("id desc");
        PaginationResultVO<AppUpdate> resultVO = appUpdateService.findListByPage(query);
        return getSuccessResponseVO(resultVO);
    }


    /*
     * 保存版本更新的相关信息
     */
    @PostMapping("/saveUpdate")
    @GlobalInterceptor(checkAdmin = true)
    //改成这样接收需要修改前端
    /*
const formData = new FormData();

// 1. 添加 JSON 数据
const dto = {
  id: 1,
  version: "1.0.0",
  updateDesc: "描述",
  fileType: 1,
  outerLink: ""
};
formData.append("appUpdateDTO", new Blob(
  [JSON.stringify(dto)],
  { type: "application/json" }  // 关键！
));

// 2. 添加文件
formData.append("file", fileInput.files[0]);

// 3. 发送请求
axios.post("/saveUpdate", formData);
     */

//    public ResponseVO saveUpdate(@RequestPart("appUpdateDTO") @Valid AppUpdateDTO appUpdateDTO, @RequestPart(value = "file", required = false) MultipartFile file) throws IOException {
//        AppUpdate appUpdate = new AppUpdate();
//        BeanUtils.copyProperties(appUpdateDTO, appUpdate);
//        appUpdateService.saveUpdate(appUpdate, file);
//        return getSuccessResponseVO(null);
//    }

   // 原来的,不使用updateDTO
     public ResponseVO saveUpdate(Integer id,
                                 @NotEmpty String version,
                                 @NotEmpty String updateDesc,
                                 @NotNull Integer fileType,
                                 String outerLink,//外链
                                 MultipartFile file) throws IOException {
        AppUpdate appUpdate = new AppUpdate();
        appUpdate.setId(id);
        appUpdate.setVersion(version);
        appUpdate.setUpdateDesc(updateDesc);
        appUpdate.setFileType(fileType);
        appUpdate.setOuterLink(outerLink);
        appUpdateService.saveUpdate(appUpdate, file);
        return getSuccessResponseVO(null);
    }


    /**
     * 删除版本更新信息
     */
    @RequestMapping("/delUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO delUpdate(@NotNull Integer id) {
        appUpdateService.deleteAppUpdateById(id);
        return getSuccessResponseVO(null);
    }

    /**
     * 发布版本更新信息
     * @param id 版本
     * @param status 状态
     * @param grayscaleUid 灰度用户ID
     *
     */
    @RequestMapping("/postUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public ResponseVO postUpdate(@NotNull Integer id, @NotNull Integer status, String grayscaleUid) {
        appUpdateService.postUpdate(id, status, grayscaleUid);
        return getSuccessResponseVO(null);
    }
}